using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace StupidSpaceThing
{
	internal class SubSurface
	{
		static public int[] Indices;
		static public VertexDeclaration VertexDeclaration;

		public VertexPositionNormalTexture[] Vertices;
		public readonly int PrimitiveCount = 2;
		public float Height = -5.0f;
		public Texture2D Background, Foreground;
		public Effect Effect;

		public static Texture2D _Background, _Foreground;
		public static Effect _Effect;

		static private GraphicsDevice Device;
		private Vector2 fgPos = new Vector2();
		private Vector2 bgPos = new Vector2();

		const float TopBorder = 100.0f;
		const float SideBorder = 80.0f;
		const float BottomBorder = 100.0f;
		private readonly Vector2 fgSpeed = new Vector2(0.00001f, 0.00003f);
		private readonly Vector2 bgSpeed = new Vector2(0.00002f, -0.00001f);

		static Vector2 textureUpperLeft = new Vector2(0.0f, 0.0f);
		static Vector2 textureUpperRight = new Vector2(1.0f, 0.0f);
		static Vector2 textureLowerLeft = new Vector2(0.0f, 1.0f);
		static Vector2 textureLowerRight = new Vector2(1.0f, 1.0f);

		static public void Init(GraphicsDevice device) {
			Device = device;
			VertexDeclaration = new VertexDeclaration(device, VertexPositionNormalTexture.VertexElements);
			Indices = new int[6];

			Indices[0] = 0;
			Indices[1] = 1;
			Indices[2] = 2;
			Indices[3] = 2;
			Indices[4] = 3;
			Indices[5] = 0;
		}

		public SubSurface(Map map) {
			LoadTextures(_Background, _Foreground);
			LoadEffect(_Effect);
			Vertices = new VertexPositionNormalTexture[4];
			UpdatePosition(map);
		}

		public void UpdatePosition(Map map) {
			//BottomRight
			Vertices[0] = new VertexPositionNormalTexture(map.BottomRight - new Vector3(SideBorder, -Height, BottomBorder), Vector3.Up, textureUpperLeft);
			//BottomLeft
			Vertices[1] = new VertexPositionNormalTexture(map.BottomLeft - new Vector3(-SideBorder, -Height, BottomBorder), Vector3.Up, textureUpperRight);
			//TopLeft
			Vertices[2] = new VertexPositionNormalTexture(map.TopLeft - new Vector3(-SideBorder, -Height, -TopBorder), Vector3.Up, textureLowerRight);
			//TopRight-
			Vertices[3] = new VertexPositionNormalTexture(map.TopRight - new Vector3(SideBorder, -Height, -TopBorder), Vector3.Up, textureLowerLeft);
		}

		public void LoadTextures(Texture2D background, Texture2D foreground) {
			Background = background;
			Foreground = foreground;
		}

		public void LoadEffect(Effect effect) {
			Effect = effect;
		}

		public void Draw(RealGameTime gameTime, Camera camera) {
			fgPos += fgSpeed * gameTime.ElapsedGameTime.Milliseconds;
			bgPos += bgSpeed * gameTime.ElapsedGameTime.Milliseconds;
			Effect.Parameters["World"].SetValue(Matrix.Identity);
			Effect.Parameters["View"].SetValue(camera.View);
			Effect.Parameters["Projection"].SetValue(camera.Projection);
			Effect.Parameters["background"].SetValue(Background);
			Effect.Parameters["foreground"].SetValue(Foreground);
			Effect.Parameters["bgPos"].SetValue(bgPos);
			Effect.Parameters["fgPos"].SetValue(fgPos);

			Device.VertexDeclaration = VertexDeclaration;
			Effect.Begin();
			foreach( EffectPass pass in Effect.CurrentTechnique.Passes ) {
				pass.Begin();
				Device.DrawUserIndexedPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, Vertices, 0, Vertices.Length, SubSurface.Indices, 0, PrimitiveCount);
				pass.End();
			}
			Effect.End();
		}

	}
}

